package cn.rongdao.jrkabao.ui.cashadvance.uicomponent;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRDisplayMetrics;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.CreditAndDepositCardBean;
import cn.rongdao.jrkabao.bean.deposit.MyLoanBean;

/**
 * Created by jack on 17/5/10
 */

public class SelectCardUIComponent extends RelativeLayout {
    public SelectCardUIComponent(Context context) {
        super(context);
    }

    public SelectCardUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SelectCardUIComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private ImageView ivAvatar;
    private TextView tvTitle;
    private TextView tvContent;
    private View viewLine;
    private TextView tvUpdate;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ivAvatar = (ImageView) findViewById(R.id.isc_iv_avatar);
        tvTitle = (TextView) findViewById(R.id.isc_tv_name);
        tvContent = (TextView) findViewById(R.id.isc_tv_right);
        viewLine = findViewById(R.id.isc_view_line);
        tvUpdate = (TextView) findViewById(R.id.isc_tv_new_version);
    }

    private void setTitleAndAvatarMargin(float marginLeft) {
        RelativeLayout.LayoutParams lpTitle = (LayoutParams) tvTitle.getLayoutParams();
        lpTitle.setMargins(FRDisplayMetrics.dp2px(getContext(), marginLeft), 0, 0, 0);
        tvTitle.setLayoutParams(lpTitle);
    }

    public void setDialogSelectCardContent(CreditAndDepositCardBean data) {
        FRImageLoad.loadImage(data.getBank_icon(), ivAvatar);
        setTitleAndAvatarMargin(10);
        if (!FRString.isEmpty(data.getIs_default()) && data.getIs_default().equals("1")) {
            setRightDrawable(R.drawable.icon_select_credit_card);
        }
        tvTitle.setText(FRCollection.appendAsString(data.getBank_name(), "(", getBankNumberLast(data.getBank_number()), ")", ""));
        tvTitle.setTextColor(ContextCompat.getColor(getContext(), R.color.c666666));
        viewLine.setVisibility(View.VISIBLE);
    }

    private String getBankNumberLast(String bankNumber) {
        if (FRString.isEmpty(bankNumber)) {
            return FRString.EMPTY_STRING;
        }
        if (bankNumber.length() < 4) {
            return bankNumber;
        }
        return bankNumber.substring(bankNumber.length() - 4, bankNumber.length());
    }

    public void setMyLoanContent(MyLoanBean data) {
        FRImageLoad.loadImage(data.getResId(), ivAvatar);
        tvTitle.setText(FRCollection.appendAsString(data.getName(), " (", FRString.valueOf(data.getCount()), ")", ""));
        setTitleAndAvatarMargin(10);
        setRightDrawable(R.drawable.icon_arrow_gray);
    }

    public void setAboutUsContent() {
        ivAvatar.setImageResource(R.drawable.icon_about_us);
        tvTitle.setText("关于我们");
        setTitleAndAvatarMargin(10);
        setRightDrawable(R.drawable.icon_arrow_gray);
    }

    public void setCustomServiceContent() {
        ivAvatar.setImageResource(R.drawable.icon_contact_service);
        tvTitle.setText("联系客服");
        setTitleAndAvatarMargin(10);
        setRightDrawable(R.drawable.icon_arrow_gray);
    }

    public void setVersionInfoTitle() {
        tvTitle.setText("版本更新");
        ivAvatar.setImageResource(R.drawable.icon_current_version);
        setTitleAndAvatarMargin(10);
    }


    public void setModifyPwd() {
        tvTitle.setText("修改密码");
        ivAvatar.setImageResource(R.drawable.icon_password);
        setTitleAndAvatarMargin(10);
        setRightDrawable(R.drawable.icon_arrow_gray);
    }

    public void setVersionInfoContent(boolean isHaveNewVersion) {
        if (isHaveNewVersion) {
            tvUpdate.setVisibility(View.VISIBLE);
        } else {
            tvContent.setText("当前已是最新版");
            tvContent.setTextColor(ContextCompat.getColor(getContext(), R.color.c999999));
            setRightDrawable(R.drawable.icon_arrow_gray);
        }
    }

    public void setTitleAndContent(String title, String content) {
        ivAvatar.setVisibility(View.GONE);
        tvTitle.setText(title);
        setTitleAndAvatarMargin(20);
        tvTitle.setTextSize(14);

        tvContent.setText(content);
        setContentRightMargin(20);
    }

    public void setTitleAndContentBold(String title, String content) {
        ivAvatar.setVisibility(View.GONE);
        tvTitle.setText(title);
        tvTitle.setTypeface(Typeface.DEFAULT_BOLD);
        tvContent.setText(content);
        setTitleAndAvatarMargin(15);
        tvContent.setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
        tvContent.setTypeface(Typeface.DEFAULT_BOLD);
    }

    private void setContentRightMargin(float marginRight) {
        RelativeLayout.LayoutParams lpContent = (LayoutParams) tvContent.getLayoutParams();
        lpContent.setMargins(0, 0, FRDisplayMetrics.dp2px(getContext(), marginRight), 0);
        tvContent.setLayoutParams(lpContent);
    }

    public void setRightDrawable(int drawableRes) {
        tvContent.setVisibility(View.VISIBLE);
        tvContent.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRes, 0);
        setContentRightMargin(20);
    }
}